package com.demo2do.fms.admin.web.controller.query;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.query.ManufacturerQuery;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.entity.data.car.Manufacturer;

/**
 *  制造商辅助查询
 *  
 * @author Wy
 *
 */
@Controller
@RequestMapping("/manufacturers")
public class ManufacturerQueryController {
	
	 @Autowired
	 private DataService<Manufacturer> dataService;
	
	/**
	 * 制造商失去焦点查询
	 * 
	 * @param code
	 * @return
	 */
    @RequestMapping(value = "/query", method = RequestMethod.GET, params = "code" ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onQuery(@RequestParam("code") String code) {
        
        HttpResult httpResult = new HttpResult();
        
        Manufacturer manufacturer = dataService.load(Manufacturer.class, code , "(code =:code OR name =:code)");
        
        if(manufacturer != null){
            
            httpResult.success().data("id", manufacturer.getId())
                                .data("code", manufacturer.getCode())
                                .data("name", manufacturer.getName());
            
            return httpResult;
        } 
        
        return httpResult.fail("400").message("查询失败");
    }
    
    /**
     * 弹窗查询
     * 
     * @return
     */
    @RequestMapping("/query")
    public String modalQuery() {
        return "query/manufacturer-query-modal";
    }
    
   /**
    * 制造商modal查询
    * 
    * @param manufacturerQuery
    * @param request
    * @param page
    * @return
    */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "query" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onModalQuery(ManufacturerQuery manufacturerQuery, 
                                                 HttpServletRequest request, 
                                                 Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        int totals = dataService.count(Manufacturer.class, manufacturerQuery.getFilter());
        
        if(totals > 0){
            
            List<Manufacturer> manufacturers = dataService.search(Manufacturer.class, manufacturerQuery, page.with(totals));
            
            httpResult.success().data("list", manufacturers)
                                .data("page", page)
                                .data("queryString", WebUtils.extractQueryString(request, "query", "page"));
        }else {
        	
            httpResult.fail("500").message("查询结果为空");
        }
        
        return httpResult;
        
    }

}
